package com.pictavore.plugin

import java.lang.reflect.Constructor
import com.pictavore.PictavoreApp
import com.pictavore.Pictavore
import com.google.inject.Singleton
import com.google.inject.Inject
import com.google.inject.Injector

/**
 *
 * @author caleb
 */
@Singleton
class PluginManager {
    private Set<Plugin> plugins
    private Set<Class<Plugin>> pluginClasses

    @Inject Pictavore pictavore
    @Inject Injector injector

    PluginManager() {
        def classFinder = new ClassFinder()
        pluginClasses = (classFinder.findSubclasses("com.pictavore.plugin.Plugin") as Set<Class<Plugin>>)
    }

    public Set<Plugin> getPlugins() {
        if (plugins == null) loadPlugins()
        plugins
    }

	private void loadPlugins() {
        plugins = new HashSet<Plugin>()

        // instantiate the plugins
        pluginClasses.each { Class pluginClass ->
            if (! pluginClass.isInterface()) {
                def plugin = injector.getInstance(pluginClass)
                plugin.load()
                plugins << plugin
            }
        }
    }
}

